---
- name: start httpd (provided in the apache role)
  service: name=httpd state=started

- name: ensure packages required for execdb are installed (yum)
  package: name={{ item }} state=present
  with_items:
    - execdb
    - mod_wsgi
    - python-psycopg2
    - libsemanage-python
  when: ansible_distribution_major_version|int < 22

- name: ensure packages required for execdb are installed (dnf)
  dnf: name={{ item }} state=present enablerepo={{ extra_enablerepos }}
  with_items:
    - execdb
    - mod_wsgi
    - python-psycopg2
    - libsemanage-python
  when: ansible_distribution_major_version|int > 21 and ansible_cmdline.ostree is not defined

- name: ensure database is created
  delegate_to: "{{ execdb_db_host_machine }}"
  become_user: postgres
  become: true
  postgresql_db: db={{ execdb_db_name }}

- name: ensure execdb db user has access to database
  delegate_to: "{{ execdb_db_host_machine }}"
  become_user: postgres
  become: true
  postgresql_user: db={{ execdb_db_name }} user={{ execdb_db_user }} password={{ execdb_db_password }} role_attr_flags=NOSUPERUSER

- name: ensure selinux lets httpd talk to postgres
  seboolean: name=httpd_can_network_connect_db persistent=yes state=yes

- name: generate execdb config
  template: src=settings.py.j2 dest=/etc/execdb/settings.py owner=root group=root mode=0644
  notify:
    - reload httpd

- name: generate execdb apache config
  template: src=execdb.conf.j2 dest=/etc/httpd/conf.d/execdb.conf owner=root group=root mode=0644
  notify:
    - reload httpd

- name: generate alembic.ini
  template: src=alembic.ini.j2 dest=/usr/share/execdb/alembic.ini owner=root group=root mode=0644

- name: initialize execdb database
  shell: PROD='true' execdb init_db

- name: initialize alembic
  shell: PROD='true' execdb init_alembic

- name: upgrade execdb database via alembic
  shell: PROD='true' execdb upgrade_db
